package com.molinari.coding.activities.home;

import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;

import com.molinari.androidstructure.listener.OnClickApriActivity;
import com.molinari.coding.ControllerCoding;
import com.molinari.coding.R;
import com.molinari.coding.activities.CodingBaseActivity;
import com.molinari.coding.activities.addconto.AggiungiContoActivity;
import com.molinari.coding.codici.Codici;
import com.molinari.coding.dati.domain.Utenti;

public class HomeUtenteActivity extends CodingBaseActivity {

	Spinner key1 = null;
	Spinner key2 = null;
	Spinner key3 = null;
	Spinner index1 = null;
	Spinner index2 = null;
	Spinner index3 = null;
	TextView text = null;
	Button cerca = null;
	private Spinner conto;
	private Button addEdit;
	private TextView textConto;
	
	@Override
	protected int getIdLayout() {
		return R.layout.home;
	}

	@Override
	protected void onCreateCustom(Bundle savedInstanceState) throws Exception {
		
		ArrayAdapter<String> key = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, Codici.keys);
		ArrayAdapter<String> index = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, Codici.indexs);
		
		key1 = (Spinner) load(key1, R.id.key1);
		key1.setAdapter(key);
		
		index1 = (Spinner) load(index1, R.id.index1);
		index1.setAdapter(index);
		
		key2 = (Spinner) load(key2, R.id.key2);
		key2.setAdapter(key);
		
		index2 = (Spinner) load(index2, R.id.index2);
		index2.setAdapter(index);
		
		key3 = (Spinner) load(key3, R.id.key3);
		key3.setAdapter(key);
		
		index3 = (Spinner) load(index3, R.id.index3);
		index3.setAdapter(index);
		
		addEdit = loadButton(addEdit, R.id.btnHomeAddOrEdit);
		textConto = (TextView) load(textConto, R.id.textNumber);
		
		boolean nonCreato = !ControllerCoding.hasConto(getApplicationContext());
		if(nonCreato){
			addEdit.setText(R.string.add);
			textConto.setText(R.string.codNonIns);
		}else{
			addEdit.setText(R.string.edit);
			textConto.setText(R.string.codPresenti);
		}
		addEdit.setOnClickListener(new OnClickApriActivity(AggiungiContoActivity.class, this));
		
		cerca = (Button) load(cerca, R.id.btnHomeCerca);
		
		cerca.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				text = (TextView) load(text, R.id.testo);
				Utenti utente = ControllerCoding.getSingleton().getUtenteLogin();
				if(utente != null){
					calcolaCodici(text, key1, index1, key2, index2, key3, index3, (Spinner) conto);
				}
			}
		});
		
	}

	protected void calcolaCodici(TextView text, 
								 Spinner key1,
								 Spinner index1, 
								 Spinner key2, 
								 Spinner index2, 
								 Spinner key3,
								 Spinner index3, 
								 Spinner conto) {
		
				String txtKey1 = (String) key1.getSelectedItem();
				String txtKey2 = (String) key2.getSelectedItem();
				String txtKey3 = (String) key3.getSelectedItem();
				String txtIndex1 = (String) index1.getSelectedItem();
				String txtIndex2 = (String) index2.getSelectedItem();
				String txtIndex3 = (String) index3.getSelectedItem();
				
				Codici codici = Codici.getCodiciByConto(this);
				String[] allValue = codici.getAllValue(txtKey1, txtIndex1, txtKey2, txtIndex2, txtKey3, txtIndex3);
				String codiciString = allValue[0] +" - " + allValue[1] + " - " + allValue[2]; 
				text.setText(codiciString);
	}

	@Override
	public int getIdMenu() {
		return R.menu.main;
	}
}
